package trees.tree

class ImmutableNode(val children:List[AbstractLeaf], val treeStart:Int,
                     val treeEnd:Int, val idStart:Int,
                     val idEnd:Int)extends AbstractNode(children,
                                                                treeStart,treeEnd,idStart,idEnd){

    lazy val ids:List[Int]=children.foldLeft(List[Int]()){ (childs:List[Int], n) => childs ::: n.getLeafs() }
    lazy val labels:List[String]=ids map ((a)=>IdFactory.getLabel(a))
    lazy val size:Int=ids.size
    
    def getLeafs():List[Int] = {
        ids
    }

    override def equals(b:Any):Boolean = b match{
        case l:Leaf => false
        case mn:MutableNode =>
            var same:Boolean = (mn.children.size == this.children.size)
            var i:Int=0
            while(same){
                same= mn.children(i).equals(this.children(i))
                i += 1
            }
            same
        case in:ImmutableNode =>
            var same:Boolean = (in.children.size == this.children.size)
            var i:Int=0
            while(same){
                same= in.children(i).equals(this.children(i))
                i += 1
            }
            same
        case an:AbstractNode => false
        case _ => false
    }

    override def deepCopy():AbstractLeaf = {
        new ImmutableNode(children.map(_.deepCopy),treeStart,treeEnd,idStart,idEnd)
    }

    override def changeMutability():AbstractLeaf = {
        new MutableNode(children.map(_.changeMutability),treeStart,treeEnd,idStart,idEnd)
    }

    override def toString() = {
        var treeString = "("
        for (child <- children){
            if (child.toString != "")
                treeString += child.toString +","
        }
        treeString.substring(0, treeString.length-1) + ")"
    }

    override def getMinId():Int=children(0).getMinId
}
